package com.ougy.customcomponent;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ougy.customcomponent.util.MoveBg;

public class TabSwitchView extends LinearLayout implements OnClickListener,
		OnFocusChangeListener {
	private static final String TAG = "TabSwitchView";
	private LinearLayout area, tab;
	private RelativeLayout tabWpr;
	private TextView showText, frontText;
	private TextView[] childViews;
	private LinearLayout[] childWrapViews;
	private boolean lockedA, lockedB;
	private int count;
	private int singleWidth, singleHeight;
	private int startX;
	private Drawable[][] imgArr;
	private OnClickListener listener,mListener;
	private Drawable currentTabBg, normalTabBg, showAreaBg, tabLine, topLine;
	private Paint mPaint;
	private String[] mContent;

	public TabSwitchView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mPaint = new Paint();
		TypedArray a = context.obtainStyledAttributes(attrs,
				R.styleable.tabs_switch);
		normalTabBg = a.getDrawable(R.styleable.tabs_switch_normal_tab_bg);
		// noSelectBg=a.getDrawable(R.styleable.tabs_switch_no_select_bg);
		// showAreaBg=a.getDrawable(R.styleable.tabs_switch_show_area_bg);
		initViews(context);
		a.recycle();

	}

	private void initViews(Context context) {
		tab = new LinearLayout(context);
		area = new LinearLayout(context);
		showText = new TextView(context);
		frontText = new TextView(context);
		tabWpr = new RelativeLayout(context);
	}

	public TabSwitchView(Context context) {
		super(context);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

		if (lockedA == false) {
			customViews();
			lockedA = true;
		}

		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}

	private void drawVerticalLine() {
		tabLine = new Drawable() {

			@Override
			public void setColorFilter(ColorFilter cf) {

			}

			@Override
			public void setAlpha(int alpha) {

			}

			@Override
			public int getOpacity() {
				return 0;
			}

			@Override
			public void draw(Canvas canvas) {
				mPaint.setColor(Color.rgb(218, 218, 218));
				mPaint.setStrokeWidth(2);
				mPaint.setAntiAlias(true);
				canvas.drawLine(0, -singleHeight, 0, singleHeight, mPaint);
			}
		};
	}

	private void drawHrRedLine() {
		topLine = new Drawable() {

			@Override
			public void draw(Canvas canvas) {
				mPaint.setColor(Color.RED);
				mPaint.setStrokeWidth(5);
				mPaint.setAntiAlias(true);
				canvas.drawLine(-singleWidth, 0, singleWidth, 0, mPaint);
			}

			@Override
			public int getOpacity() {
				return 0;
			}

			@Override
			public void setAlpha(int alpha) {

			}

			@Override
			public void setColorFilter(ColorFilter cf) {

			}

		};
	}

	@Override
	public void setOnClickListener(OnClickListener listener) {
		if (listener != null) {
			this.listener = listener;
		}
		if(mListener==null){
			mListener=this;
		}
		Log.i(TAG,"count:"+count);
		for (int i = 0; i < count; i++) {
			childViews[i].setOnClickListener(this);
			childViews[i].setOnFocusChangeListener(this);
		}
	}


	private void customViews() {

		count = this.getChildCount();
		childViews = new TextView[count];
		childWrapViews=new LinearLayout[count];
		imgArr = new Drawable[count][];
		for (int i = 0; i < count; i++) {
			try {
				childWrapViews[i]=new LinearLayout(getContext());
				LayoutParams paramC=new LinearLayout.LayoutParams(
						LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT,1.0f); 
				childWrapViews[i].setLayoutParams(paramC);
				childWrapViews[i].setGravity(Gravity.CENTER);
				childWrapViews[i].setFocusable(false);
//				childWrapViews[i].setTag(i);
//				childWrapViews[i].setClickable(true);
//				childWrapViews[i].setFocusable(true);
//				childWrapViews[i].setFocusableInTouchMode(true);
//				childWrapViews[i].setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);
				
				childViews[i] = (TextView) getChildAt(i);
				LayoutParams paramL = new LinearLayout.LayoutParams(
						LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
				childViews[i].setLayoutParams(paramL);
				childViews[i].setGravity(Gravity.CENTER);
				
				paramL.bottomMargin=15;
				paramL.topMargin=15;
				paramL.rightMargin=15;
				paramL.leftMargin=15;
				
				childViews[i].setTag(i);
				childViews[i].setFocusable(true);
				childViews[i].setClickable(true);
				childViews[i].setFocusableInTouchMode(true);

				if (i == 0) {
					childWrapViews[i].setBackgroundResource(R.drawable.shape_lefttop_radius);
				}
				imgArr[i] = childViews[i].getCompoundDrawables();
				
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		removeAllViews();
		setOrientation(LinearLayout.VERTICAL);
		tab.setLayoutParams(new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		tab.setGravity(Gravity.CENTER_HORIZONTAL);
		for (int i = 0; i < count; i++) {
			childWrapViews[i].addView(childViews[i]);
			tab.addView(childWrapViews[i]);
		}
		tab.setBackgroundResource(R.drawable.shape_top_radius_gray);
		area.setLayoutParams(new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		showText.setPadding(25, 25, 25, 25);
		showText.setTextColor(Color.BLACK);
		if (mContent != null) {
			showText.setText(mContent[0]);
		}
		area.setBackgroundResource(R.drawable.shape_bottom_radius);
		area.addView(showText);
		tabWpr.setLayoutParams(new RelativeLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		tabWpr.addView(tab);

		addView(tabWpr);
		addView(area);

		setOnClickListener(null);

	}

	@Override
	public void onClick(View v) {
		if (mContent != null) {
			int tag = (Integer) v.getTag();
			showText.setText(mContent[tag]);
		}
		if (listener != null) {
			listener.onClick(v);
		}

	}

	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		if (hasFocus == true) {
			int tag = (Integer) v.getTag();
			for (int i = 0; i < count; i++) {
				if (i == tag) {
					if(tag==0){
						childWrapViews[tag].setBackgroundResource(R.drawable.shape_lefttop_radius);
					}else if(tag==(count-1)){
						childWrapViews[tag].setBackgroundResource(R.drawable.shape_righttop_radius);
					}else{
						childWrapViews[tag].setBackgroundResource(R.drawable.shape_noradius);
					}
					TextView test = childViews[tag];
					boolean a = test.isClickable();
					boolean b = test.isEnabled();
					boolean c = test.isFocusable();
					boolean d = test.isFocused();
					boolean e = test.isPressed();
					boolean f = test.isSelected();
					Log.i(TAG, "Clickable:" + a + ",Enabled:" + b
							+ ",Focusable:" + c + ",Focused:" + d + ",Pressed:"
							+ e + ",selected:" + f);
				} else {
					childWrapViews[i].setBackgroundResource(0);
				}
			}
			if(mListener!=null){
				mListener.onClick(v);
			}
			if(listener!=null){
				listener.onClick(v);
			}
//			if(mListener!=null){
//				mListener.onClick(v);
//			}
		}
	}

	public void setTabContent(String[] content) {
		if (content == null) {
			throw new NullPointerException(
					"The Method:setTabContent() of paramters is empty!");
		} else if (content instanceof String[]) {
			int l = content.length;
			mContent = new String[l];
			for (int i = 0; i < l; i++) {
				mContent = content;
			}
		} else {
			throw new RuntimeException(
					"The Method:setTabContent() of paramters is unmatched!");
		}
	}


	public TextView getShowText() {
		return showText;
	}

}
